package com.yihuier.yihuier.Service.UserManager;

import com.yihuier.yihuier.Entity.UserManager.Manager;
import com.yihuier.yihuier.Mapper.UserManager.ManagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

@Service
public class ManagerService {
    @Autowired
    private ManagerMapper managerMapper;


    /**
     * 根据公司id得到管理员编号数组
     */
    public int[] getManagerIdsByCompanyId(int company_id) {
        return managerMapper.selectManagerIdsByCompanyId(company_id);
    }

    /**
     * 根据管理员编号得到公司id
     */
    public int getCompanyIdByManagerId(String manager_id){
        return managerMapper.selectCompanyIdByManagerId(manager_id);
    }

    /**
     * 根据管理员账号密码获取管理员Id
     * */
    public String getManagerIdByAccountAndPassword(String manager_account,String user_password){
        return managerMapper.selectManagerIdByAccountAndPassword(manager_account,user_password);
    }

    /**
     * 获取管理员的公司id判断其是超级管理员与否
     * */
    public String judgeSuperManager(String manager_id){
        return managerMapper.judgeSuperManager(manager_id);
    }

    /**
     * 找到本公司的智能管理员Id
     * */
    public int getAIManagerId(int company_id){
        return managerMapper.selectAIManager(company_id);
    }

    /**
     * 添加公司时添加智能管理员
     * */
    public int addAIManager(int company_id){
        String yearLast = new SimpleDateFormat("yy", Locale.CHINESE).format(Calendar.getInstance().getTime());
        String seqCompanyId = String.format("%03d", company_id);
        String seqManagerId = "01";
        String manager_account = yearLast+seqCompanyId+seqManagerId;
        String manager_username = company_id+"智能管理员";
        String Md5EncodePassword = DigestUtils.md5DigestAsHex(manager_account.getBytes());//md5加密密码
        return managerMapper.insertManager(manager_account,manager_username, Md5EncodePassword,company_id,"1");
    }

    public int addManager(String manager_username,int company_id,String manager_account){
        String Md5EncodePassword = DigestUtils.md5DigestAsHex(manager_account.getBytes());//md5加密密码
        return managerMapper.insertManager(manager_account,manager_username, Md5EncodePassword,company_id,"0");
    }

    public String beforeAddManager(int company_id){
        String yearLast = new SimpleDateFormat("yy", Locale.CHINESE).format(Calendar.getInstance().getTime());
        String seqCompanyId = String.format("%03d", company_id);
        int seqManagerId = managerMapper.selectSumOfManagersInCompany(company_id)+1;
        String manager_account = yearLast+seqCompanyId+seqManagerId;
        return manager_account;
    }


    /**
     * 根据管理员id获取管理员用户名
     * */
    public String getManagerNameByManagerId(String manager_id){
        return managerMapper.selectManagerNameByManagerId(manager_id);
    }

    /**
     * 超级管理员获得所有管理员信息
     * */
    public List<Manager> selectManagersForSuperManager(){
        return managerMapper.selectManagersForSuperManager();
    }

    /**
     * 管理员阐述/取消删除操作
     * */
    public int setManagerStatus(int manager_id,int manager_status){
        return managerMapper.setManagerStatus(manager_id,manager_status);
    }
}
